package deleteface.bank.jqsoft.cn.deleteface.webservice;




import java.util.concurrent.TimeUnit;

import deleteface.bank.jqsoft.cn.deleteface.base.BphsConstants;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.fastjson.FastJsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.convert.AnnotationStrategy;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.strategy.Strategy;


public class RequestManager {
    public final static int CONNECT_TIMEOUT = 100;
    public final static int READ_TIMEOUT = 100;
    public final static int WRITE_TIMEOUT = 100;
//    public static String BASE_URL = BphsConstants.HTTP_PROTOCOL
//            + BphsConstants.HTTP_TEST_SERVER_IP + ":" + BphsConstants.HTTP_TEST_SERVER_PORT
//            + "/";
public static String BASE_URL = "";

    //http://192.168.45.240:9998/WebService/AndrewsWebService.asmx
    //http://10.1.74.63:8087/gdws-core/WebService/Andrews/AndrewsWebService?wsdl
//    public static String BASE_URL = BphsConstants.HTTP_PROTOCOL
//            + "10.1.74.63" + ":" +"8087"
//            + "/";

    public Retrofit mRetrofit;
    private static RequestManager mRequestManager;//管理者实例
    private OkHttpClient mClient;//OkHttpClient实例
    public ServiceStore mServiceStore;//请求接口
    public static Boolean isSras = false;

    //   public ServiceStore2 mServiceStore;//请求接口
    private RequestManager(String baseUrl) {

        init(baseUrl);

    }

    //单例模式，对提供管理者实例
    public static RequestManager getInstance(String url) {
//        BASE_URL = BphsConstants.HTTP_PROTOCOL + BphsConstants.HTTP_TEST_SERVER_IP + ":" + BphsConstants.HTTP_TEST_SERVER_PORT + "/";
        BASE_URL=url;
        if (isSras) {
            mRequestManager = new RequestManager(BASE_URL);
            isSras = false;
        } else {
            if (mRequestManager == null) {
                synchronized (RequestManager.class) {
                    if (mRequestManager == null) {
                        mRequestManager = new RequestManager(BASE_URL);
                    }
                }
            }
            mRequestManager = new RequestManager(BASE_URL);
        }
        return mRequestManager;
    }

    //单例模式，对提供管理者实例
    public static RequestManager getInstance() {
//        BASE_URL = BphsConstants.HTTP_PROTOCOL + BphsConstants.HTTP_TEST_SERVER_IP + ":" + BphsConstants.HTTP_TEST_SERVER_PORT + "/";

        BASE_URL = BphsConstants.HTTP_PROTOCOL + "218.22.110.114" + ":" + "9099" + "/";
        if (isSras) {
            mRequestManager = new RequestManager(BASE_URL);
            isSras = false;
        } else {
            if (mRequestManager == null) {
                synchronized (RequestManager.class) {
                    if (mRequestManager == null) {
                        mRequestManager = new RequestManager(BASE_URL);
                    }
                }
            }
            mRequestManager = new RequestManager(BASE_URL);
        }
        return mRequestManager;
    }

    private void init(String baseUrl) {
        Strategy strategy = new AnnotationStrategy();
        Serializer serializer = new Persister(strategy);
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
//        if(BphsConstants.appweburltype == 2){
//            builder.addNetworkInterceptor(new NetChangeInterceptorNew());
//        }else {
//            builder.addNetworkInterceptor(new NetChangeInterceptor());
//        }
//        builder.addNetworkInterceptor(new NetChangeInterceptor());

        builder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
        builder.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS);
        builder.writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS);
        builder.connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        mClient = builder.build();

        mRetrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(FastJsonConverterFactory.create())
  //              .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(SimpleXmlConverterFactory.create(serializer))
                .client(mClient)
                .build();
        mServiceStore = mRetrofit.create(ServiceStore.class);
    }

    public interface onRequestCallBack {
        void onSuccess(String msg);

        void onError(String msg);
    }
}
